﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Addins.UI.Windows;
using Addins.UI.ViewModel;

namespace Addins.UI.View
{
    public class WindowBaseView : WindowBase
    {
        public WindowBaseView()
        {
            this.DataContextChanged += new DependencyPropertyChangedEventHandler(this.OnDataContextChanged);
        }

        private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue is IRequestCloseViewModel)
            {
                // if the new datacontext supports the IRequestCloseViewModel we can use
                // the event to be notified when the associated viewmodel wants to close
                // the window
                ((IRequestCloseViewModel)e.NewValue).RequestClose += this.OnForceClose;
            }
        }

        private void OnForceClose(object sender, EventArgs e)
        {
            ((IRequestCloseViewModel)sender).RequestClose -= this.OnForceClose;
            this.ForceClose();
        }
    }
}
